package com.kelvin.oocl.testTransaction.tool;

import org.apache.commons.dbcp2.BasicDataSource;

import java.sql.Connection;
import java.sql.SQLException;

public class DBUtil {
    private static String username = "root";
    private static String pwd = "mysql";
    private static String url="jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8";
    private static String driver = "com.mysql.jdbc.Driver";
    private static BasicDataSource ds;
    private static Connection connection = null;

    static {
        ds = new BasicDataSource();
        ds.setUrl(url);
        ds.setPassword(pwd);
        ds.setUsername(username);
        ds.setDriverClassName(driver);
    }

    public static Connection getConnection(){
        if(connection==null){
            try {
                connection = ds.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }
}
